package com.sky.service.impl;

import com.sky.Entity.AddressBook;
import com.sky.context.BaseContext;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public void add(AddressBook addressBook) {
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getEmpId());
        addressBookMapper.insert(addressBook);
    }

    @Override
    public List<AddressBook> list() {
        return addressBookMapper.queryByUserId(BaseContext.getEmpId());
    }

    @Override
    public AddressBook defaultAddress() {
        return addressBookMapper.queryUserDefaultAddress(BaseContext.getEmpId());
    }

    @Override
    public void modifyAddress(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    @Override
    public void deleteAddressById(Long id) {
        addressBookMapper.deletById(id);
    }

    @Override
    public AddressBook queryAddressById(Long id) {
        return addressBookMapper.queryById(id);
    }

    @Override
    @Transactional
    public void setdefault(AddressBook addressBook) {
        addressBook.setIsDefault(1);
        addressBookMapper.updateDefaultByUserId(BaseContext.getEmpId());
        addressBookMapper.update(addressBook);
    }
}
